package cc.mrbird.febs.power.controller;


import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.power.entity.AccountBindMenuInfo;
import cc.mrbird.febs.power.entity.MenuBindAppInfo;
import cc.mrbird.febs.power.entity.MenuInfo;
import cc.mrbird.febs.power.entity.TreeNode;
import cc.mrbird.febs.power.service.IAccountBindMenuInfoService;
import cc.mrbird.febs.power.service.IMenuBindAppInfoService;
import cc.mrbird.febs.power.service.IMenuInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MrBird
 */
@RestController
@RequestMapping("/power/menu-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MenuInfoController {

    private final IMenuInfoService iMenuInfoService;

    private final IAccountBindMenuInfoService iAccountBindMenuInfoService;

    private final IMenuBindAppInfoService iMenuBindAppInfoService;

    /**
     * 分页获取所有菜单
     * @param page
     * @param menuInfo
     * @return
     */
    @GetMapping("/page")
    public R getMenuInfoByPage(Page page,MenuInfo menuInfo) {
        return R.ok(iMenuInfoService.getMenuInfoByPage(page,menuInfo));
//        return R.ok(iMenuInfoService.page(page, Wrappers.<MenuInfo>lambdaQuery()
//                .like(menuInfo.getMenuName()!=null,MenuInfo::getMenuName,menuInfo.getMenuName())
//                .eq(menuInfo.getMenuType()!=null,MenuInfo::getMenuType,menuInfo.getMenuType())));
    }

    /**
     * 获取模块菜单信息
     * @return
     */
    @GetMapping("parent")
    public R getMenuParentInfo() {
        return R.ok(iMenuInfoService.list(Wrappers.<MenuInfo>lambdaQuery().eq(MenuInfo::getParentMenuId,0)));
    }

    /**
     * 获取所有菜单信息
     * @return
     */
    @GetMapping
    public R getMenuInfo() {
        List<MenuInfo> menuInfos = new ArrayList<>();
        List<MenuInfo> menuInfoNodes = iMenuInfoService.list();
        menuInfoNodes.forEach(menuInfo -> {
            if(menuInfo.getParentMenuId()==0){
                menuInfos.add(menuInfo);
            }
            menuInfoNodes.forEach(menuInfo1 -> {
                if(menuInfo1.getParentMenuId().toString().equals(menuInfo.getMenuId().toString())) {
                    if(menuInfo.getMenuInfos() == null) {
                        menuInfo.setMenuInfos(new ArrayList<>());
                    }
                    List<MenuInfo> chmenus = iMenuInfoService.list(Wrappers.<MenuInfo>lambdaQuery().eq(MenuInfo::getParentMenuId,menuInfo1.getMenuId()));
                    if(chmenus.size()!=0){
                        menuInfo1.setMenuInfos(new ArrayList<>());
                        menuInfo1.getMenuInfos().addAll(chmenus);
                    }
                    menuInfo.getMenuInfos().add(menuInfo1);
                }
            });

        });
        return R.ok(menuInfos);
    }

    /**
     *
     * @return
     */
    @GetMapping("/tree")
    public R getMenuInfoByTree() {
        List<MenuInfo>positionInfoList=iMenuInfoService.list();
        List<TreeNode>treeNodes=new ArrayList<>();
        List<TreeNode>resultTree=new ArrayList<>();
        positionInfoList.forEach(positionInfo->{
            treeNodes.add(new TreeNode(positionInfo.getMenuName(),positionInfo.getMenuId(),positionInfo.getParentMenuId(),new ArrayList<>()));
        });
        System.out.println(treeNodes);
        treeNodes.forEach(treeNode->{
            if(treeNode.getParentId()==0){
                resultTree.add(treeNode);
            }
            treeNodes.forEach(ItreeNode->{
                if(treeNode.getKey().equals(ItreeNode.getParentId())){
                    if(treeNode.getChildren()==null){
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.getChildren().add(ItreeNode);
                }
            });
        });

        return R.ok(resultTree);
    }

    /**
     * 根据账户编号获取菜单树
     * @param id
     * @return
     */
    @GetMapping("/menuInfo/{id}")
    public R getMenuInfoByAccount(@PathVariable("id") Integer id) {
        List<AccountBindMenuInfo> bindMenuInfoList = iAccountBindMenuInfoService.list(Wrappers.<AccountBindMenuInfo>lambdaQuery()
                .eq(AccountBindMenuInfo::getAccountId,id));

        return R.ok();
    }

    /**
     * 新增菜单信息
     * @param menuInfo
     * @return
     */
    @PostMapping("/MenuInfoAdd")
    public R MenuInfoAdd(MenuInfo menuInfo,String appId) {
        long menuId = iMenuInfoService.menuBindAccount(menuInfo);
        if(menuInfo.getParentMenuId()==null){
            menuInfo.setParentMenuId(0);
        }
        if(!("").equals(appId) && appId != null){
            iMenuBindAppInfoService.save(new MenuBindAppInfo(menuId,appId));
        }
        return R.ok("ok");
    }
    @GetMapping("/mess/{menuId}")
    public R getMessageByMenu(@PathVariable("menuId") Integer id) {
        return R.ok(iMenuBindAppInfoService.getOne(Wrappers.<MenuBindAppInfo>lambdaQuery().eq(MenuBindAppInfo::getMenuId,id)));
    }
    /**
     * 修改菜单信息
     * @param menuInfo
     * @return
     */
    @PutMapping("/MenuInfoEdit")
    public R MenuInfoEdit(MenuInfo menuInfo,String appId) {
        System.out.println("==="+menuInfo);
        if(appId!=null){
            iMenuBindAppInfoService.update(Wrappers.<MenuBindAppInfo>lambdaUpdate().set(MenuBindAppInfo::getAppId,appId).eq(MenuBindAppInfo::getMenuId,menuInfo.getMenuId()));
        }
        return R.ok(iMenuInfoService.updateById(menuInfo));
    }

    /**
     * 根据ID删除记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R MenuInfoDel(@PathVariable Integer id) {
        return R.ok(iMenuInfoService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/man/{ids}")
    public R MenuInfosDel(@PathVariable("ids") List<Integer> ids) {
        return R.ok(iMenuInfoService.removeByIds(ids));
    }
}
